package com.oymn.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.oymn.myapplication.R;
import com.oymn.myapplication.entity.Goods;
import com.oymn.myapplication.utils.MyImageView;
import com.oymn.myapplication.utils.Url;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>{

    List<Goods> mDatas = new ArrayList<Goods>();
    String url = Url.goods_url;

    private Context context = null;
    private String user_phone = null;

    public HomeAdapter(Context context,String user_phone){
        this.context = context;
        this.user_phone = user_phone;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        HomeAdapter.MyViewHolder holder = new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_first,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull HomeAdapter.MyViewHolder holder, int position) {
        if(mDatas != null){
            holder.goodsName_1.setText(mDatas.get(position).getGoods_name());
            holder.goodsIntro_1.setText(mDatas.get(position).getGoods_introl());
            holder.price.setText(String.valueOf(mDatas.get(position).getGoods_price()));
        }

        //获取服务器图片
        String goods_id = String.valueOf(mDatas.get(position).getGoods_id());
        holder.imageView_1.setImageURL(url,goods_id);

        //获取图片
        //Bitmap image = ((BitmapDrawable)holder.imageView_1.getDrawable()).getBitmap();

        holder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.goods");
                intent.putExtra("goods_name",holder.goodsName_1.getText().toString());
                holder.imageView_1.setDrawingCacheEnabled(Boolean.TRUE);
                intent.putExtra("goods_img",holder.imageView_1.getDrawingCache());
                intent.putExtra("goods_price",holder.price.getText().toString());
                intent.putExtra("goods_introl",holder.goodsIntro_1.getText().toString());
                intent.putExtra("user_phone",user_phone);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }
    class MyViewHolder extends  RecyclerView.ViewHolder{

        TextView goodsName_1,goodsIntro_1,price;
        Button comment,more;
        MyImageView imageView_1;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            goodsName_1 = itemView.findViewById(R.id.goodsName_1);
            goodsIntro_1 = itemView.findViewById(R.id.goodsIntro_1);
            price = itemView.findViewById(R.id.price);
            comment = itemView.findViewById(R.id.comment);
            more = itemView.findViewById(R.id.more);

            imageView_1 = itemView.findViewById(R.id.imageView_1);

        }
    }

    public void setmDatas(List<Goods> mDatas) {
        this.mDatas = mDatas;
        this.notifyDataSetChanged();
    }
}
